package com.zhengxing.jienci.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhengxing.jienci.mybatisplus.mapper.UserMapper;
import com.zhengxing.jienci.mybatisplus.pojo.User;
import com.zhengxing.jienci.mybatisplus.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 浪遏飞舟
 * @create 2020-09-25 11:20
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper mapper;


    /**
     * 单表查询
     * @return
     */
    public List<User> query(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id","1");
        List<User> users = mapper.selectList(wrapper);
        return users;
    }

    public List<User> queryByMap(){
        Map<String, Object> map = new HashMap<>();
        map.put("id","1");
        List<User> users = mapper.selectByMap(map);
        return users;
    }

    /**
     * 根据id查询
     * @return
     */
    @Override
    public User queryById() {
        final User user = mapper.selectById(1);
        return user;
    }


    /**
     * 单表分页查询
     * @return
     */
    @Override
    public IPage<User> pageQuery() {
        IPage<User> page = new Page<>(1,1);
        IPage<User> userIPage = mapper.selectPage(page, new QueryWrapper<User>().eq("id","1"));
        return userIPage;
    }


    /**
     * 联合查询
     * @return
     */
    @Override
    public Object joinSel() {
        Map<String, Object> map = new HashMap<>();
        map.put("id","1");
        List<Map> joinList = mapper.joinSelect(map);
        return joinList;
    }

    /**
     * 联合分页查询
     * @return
     */
    @Override
    public Object pageJoinSel() {
        Page<User> page = new Page<>(1,10);
        Page<Map> mapIPage = mapper.pageJoinSel(page,1);
        return mapIPage;
    }

    /**
     * 添加
     * @return
     */
    @Override
    public int insert() {
        User user = new User();
        user.setId(4);
        user.setEmail("wwww.qqqq");
        int count = mapper.insert(user);
        return count;
    }

    /**
     * 根据id修改
     * @return
     */
    @Override
    public int editById() {
        User user = new User();
        user.setId(1);
        user.setEmail("aaaaaa");
        int count = mapper.updateById(user);
        return count;
    }

    /**
     * 修改
     * @return
     */
    @Override
    public int edit() {
        User user = new User();
        user.setEmail("fffffffffff");
        int count = mapper.update(user,new QueryWrapper<User>().eq("last_name","zzzzzzzzzzzz"));
        return count;
    }


}
